package com.sunrise.gateway.web.core;

import com.alibaba.fastjson.JSON;
import com.dap.param.StringInput;
import com.dap.utils.TraceIdUtils;
import com.sunrise.gateway.commons.CommonUtils;
import com.sunrise.gateway.commons.SlowGroupUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/****
 * 请求转发核心功能
 * @author wangmin
 */
public abstract class EngineBase {
    protected final static Logger tracelog = LoggerFactory.getLogger("tracelog");

    @Autowired
    protected ServiceCommonInterface serviceCommonInterface;
    @Autowired
    protected FillSessionInfoIntoCtrlDataInterface fillSessionInfoIntoCtrlDataInterface;


    protected static String getGroupName(HttpServletRequest req, String leaveOutGroupNamePrefix) {
        String group = CommonUtils.getGroup(req);
        if (null != leaveOutGroupNamePrefix) {
            group = group.substring(leaveOutGroupNamePrefix.length());
        }
        return group;
    }
    private static Map<String, Object> generateContext() {
        Map<String, Object> context = new HashMap<>();
        context.put("traceId", TraceIdUtils.getTraceId());
        context.put("isFrontRequest", true);
        context.put("notExecuteByServiceName", true);
        return context;
    }

    protected String executeRequestStr(String group, StringInput stringInput, boolean isPrintOutput) {
        tracelog.info("{}, 输入参数 : {}", group, JSON.toJSONString(stringInput));
        final long startTime = System.currentTimeMillis();

        Map<String, Object> context = generateContext();
        String resultStr = serviceCommonInterface.executeSunriseServiceStr(context, group, stringInput);

        if (isPrintOutput) {
            tracelog.info("{}, 输出内容 : {}", group, resultStr);
        }

        final long spendTimeMillis = System.currentTimeMillis()-startTime;
        if (spendTimeMillis >= 3500 && SlowGroupUtils.isGroupNeedLog(group)) {
            tracelog.info("request executes too long, group={}, spendTimeMillis={}", group, spendTimeMillis);
        }

        return resultStr;
    }

    protected Object executeRequestObject(String group, StringInput stringInput, boolean isPrintOutput) {
        tracelog.info("{}, 输入参数 : {}", group, JSON.toJSONString(stringInput));
        final long startTime = System.currentTimeMillis();

        Map<String, Object> context = generateContext();
        Object result = serviceCommonInterface.executeSunriseServiceObject(context, group, stringInput);

        if (isPrintOutput) {
            String resultStr = JSON.toJSONString(result);
            tracelog.info("{}, 输出内容 : {}", group, resultStr);
        }

        final long spendTimeMillis = System.currentTimeMillis()-startTime;
        if (spendTimeMillis >= 3500 && SlowGroupUtils.isGroupNeedLog(group)) {
            tracelog.info("request executes too long, group={}, spendTimeMillis={}", group, spendTimeMillis);
        }

        return result;
    }

}
